﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Configuration;
using EntityMap;
using EntityMapTest.Entities;

namespace EntityMapTest
{
    /// <summary>
    /// Summary description for EntityManagerTest
    /// </summary>
    [TestClass]
    public class EntityManagerTest
    {
        private string connString;
        private EntityMapper mapper;

        public EntityManagerTest()
        {
            connString = ConfigurationManager.ConnectionStrings["dbconn"].ConnectionString;

            mapper = new EntityMapper( connString );

            mapper.Out = Console.Out;
        }
       
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void Query_AllAddress()
        {
            IList<Address> result = (IList<Address>)mapper.ExecuteStatement<Address>( "all", null );

            Assert.IsTrue( result.Count > 0, "no Address return" );
        }

        [TestMethod]
        public void Query_Address()
        {
            Address result = (Address)mapper.ExecuteStatement<Address>( "byid", new { Id = 1 } );

            Assert.IsNotNull( result );

            Assert.IsTrue( result.City == "Salerno", "the property City is not set" );
            Assert.IsTrue( result.Country == "Italy", "the property Country is not set" );
            Assert.IsTrue( result.PostalCode == 48, "the property PostalCode is not set" );
            Assert.IsTrue( result.PrimaryAddress == "Via Pinco Pallino", "the property PrimaryAddress is not set" );
        }

        [TestMethod]
        public void Query_AllPersons()
        {
            IList<Person> result = (IList<Person>)mapper.ExecuteStatement<Person>( "all", new { Id = 1 } );

            Assert.IsTrue( result.Count > 0, "no Person return" );
        }

        [TestMethod]
        public void Query_AllPersonsBySex()
        {
            IList<Person> result = (IList<Person>)mapper.ExecuteStatement<Person>( "bysex", new { Id = 1, Sex = 'm' } );

            Assert.IsTrue( result.Count > 0, "no Person return" );
        }

        [TestMethod]
        public void Query_Person()
        {
            Person result = (Person)mapper.ExecuteStatement<Person>( "byid", new { Id = 1 } );

            Assert.IsNotNull( result );

            Assert.IsTrue( result.FirstName == "Antonio", "the property FirstName is not set" );
            Assert.IsTrue( result.LastName == "Di Motta", "the property LastName is not set" );
            Assert.IsTrue( result.Age == 38, "the property Age is not set" );
            Assert.IsTrue( result.Sex == "m", "the property Sex is not set" );
        }

        [TestMethod]
        public void Create()
        {
            Person p1 = new Person();
            p1.FirstName = "firstExample1";
            p1.LastName = "lastExample1";
            p1.Age = 34;
            p1.Sex = "m";

            bool result = (bool)mapper.ExecuteStatement<Person>( "create", p1 );

            Assert.IsTrue( result, "persist error" );
        }

        [TestMethod]
        public void Update()
        {
            Person p1 = new Person();
            p1.FirstName = "firstExample2";
            p1.LastName = "lastExample2";
            p1.Age = 35;
            p1.Id = 1;

            bool result = (bool)mapper.ExecuteStatement<Person>( "update", p1 );

            Assert.IsTrue( result, "persist error" );
        }

        [TestMethod]
        public void Delete()
        {
            Address a1 = new Address { Id = 1 };
            Person p1 = new Person { Id = 1 };

            bool result1 = (bool)mapper.ExecuteStatement<Address>( "delete", a1 );
            Assert.IsTrue( result1, "change persist error" );

            bool result2 = (bool)mapper.ExecuteStatement<Person>( "delete", p1 );
            Assert.IsTrue( result2, "change persist error" );
        }

        [TestMethod]
        public void ExecuteStored()
        {
            Person p1 = new Person();

            p1.FirstName = "firstExample3";
            p1.LastName = "lastExample3";
            p1.Age = 40;
            p1.Sex = "m";

            bool result = (bool)mapper.ExecuteStatement<Person>( "create", p1 );

            Assert.IsTrue( result, "persist error" );
        }

        //[TestMethod]
        //public void SaveEntityInfo()
        //{
        //    EntityMap.EntityMapper mapper = new EntityMapper( "" );

        //    EntityMap.Meta.EntityInfo einfo = new EntityMap.Meta.EntityInfo();
        //    einfo.aliases.Add( new EntityMap.Meta.Alias { Id = "prova", Text = "pippo" } );

        //    mapper.SaveEntityInfo( typeof( Person ), einfo );
        //}
    }
}
